﻿//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;
//using System.Threading.Tasks;
//using System.Windows;
//using System.Windows.Controls;
//using System.Windows.Data;
//using System.Windows.Documents;
//using System.Windows.Input;
//using System.Windows.Media;
//using System.Windows.Media.Imaging;
//using System.Windows.Shapes;

//using System.Drawing;
//using System.Drawing.Imaging;
//using System.Runtime.InteropServices;
//using SuporeSliplampApp.Util;

//namespace SuporeSliplampApp.Views
//{
//    /// <summary>
//    /// VideoWindow.xaml 的交互逻辑
//    /// </summary>
//    public partial class VideoWindow : Window
//    {
//        private uvcham cam_ = null;
//        private Bitmap bmp_ = null;
//        private int width_ = 0, height_ = 0;
//        private int count_ = 0;
//        public VideoWindow()
//        {
//            InitializeComponent();



//        }

//        private void OnStart(object sender, EventArgs e)
//        {
//            if (cam_ != null)
//                return;

//            uvcham.Device[] arr = uvcham.Enum();
//            if (arr.Length <= 0)
//                System.Windows.MessageBox.Show("No camera found.");
//            else
//            {
//                cam_ = uvcham.open(arr[0].id);
//                if (cam_ != null)
//                {
//                    cam_.put(uvcham.eCMD.WBMODE, 2);

//                    //checkBox1.Enabled = trackBar1.Enabled = true;
//                    //button3.Enabled = true;

//                    cam_.get(uvcham.eCMD.WIDTH | 0, out width_);
//                    cam_.get(uvcham.eCMD.HEIGHT | 0, out height_);
//                    cam_.put(uvcham.eCMD.FLIPVERT, 1);
//                    bmp_ = new Bitmap(width_, height_, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
//                    if (cam_.start(IntPtr.Zero/* Pull Mode */, (nEvent) =>
//                    {
//                        /* this is called by internal thread of uvcham.dll which is NOT the same of UI thread.
//                         * Why we use BeginInvoke, Please see:
//                         * http://msdn.microsoft.com/en-us/magazine/cc300429.aspx
//                         * http://msdn.microsoft.com/en-us/magazine/cc188732.aspx
//                         * http://stackoverflow.com/questions/1364116/avoiding-the-woes-of-invoke-begininvoke-in-cross-thread-winform-event-handling
//                         */
//                        BeginInvoke((Action)(() =>
//                        {
//                            /* this run in the UI thread */
//                            if (cam_ != null)
//                            {
//                                if (0 != (nEvent & uvcham.eEVENT.DISCONNECT))
//                                {
//                                    cam_.close();
//                                    MessageBox.Show("Camera disconnect.");
//                                }
//                                else if (0 != (nEvent & uvcham.eEVENT.ERROR))
//                                {
//                                    cam_.close();
//                                    System.Windows.MessageBox.Show("Generic error.");
//                                }
//                                else if (0 != (nEvent & uvcham.eEVENT.IMAGE))
//                                {
//                                    if (bmp_ != null)
//                                    {
//                                        try
//                                        {
//                                            //BitmapData bmpdata = bmp_.LockBits(new Rectangle(0, 0, bmp_.Width, bmp_.Height), ImageLockMode.WriteOnly, bmp_.PixelFormat);
//                                            if (bmpdata.Scan0 != IntPtr.Zero)
//                                            {
//                                                cam_.pull(bmpdata.Scan0);/* Pull Mode */
//                                                bmp_.UnlockBits(bmpdata);
//                                                image_ = bmp_;
//                                            }
//                                        }
//                                        catch (Exception ex)
//                                        {
//                                            System.Windows.MessageBox.Show(ex.ToString());
//                                        }
//                                    }
//                                }
//                            }
//                        }));
//                    }) < 0)
//                        System.Windows.MessageBox.Show("Failed to start camera.");
//                    else
//                    {
//                        //int nMin = 0, nMax = 0, nDef = 0;
//                        //cam_.range(uvcham.eCMD.EXPOTIME, out nMin, out nMax, out nDef);
//                        //trackBar1.SetRange(nMin, nMax);

//                        //UpdateExpoTime();
//                        //int val = 0;
//                        //cam_.get(uvcham.eCMD.AEXPO, out val);
//                        //checkBox1.Checked = (val != 0);
//                        //trackBar1.Enabled = (val == 0);
//                    }
//                }
//            }
//        }

//        private void OnClosing(object sender, FormClosingEventArgs e)
//        {
//            cam_?.close();
//            cam_ = null;
//        }


//    }
//}
